package com.ctgu.B.B3;

import com.ctgu.B.B3.action.*;
import com.github.oxo42.stateless4j.StateMachine;
import com.github.oxo42.stateless4j.StateMachineConfig;

/**
 * 密码验证器
 */
public class StringParserStateMachine {

    private StateMachineConfig<State, Event> config;
    private StateMachine<State, Event> stateMachine;

    public StringParserStateMachine() {
        init();
    }

    public void init() {
        // 创建状态机配置
        config = new StateMachineConfig<>();

//        sendFile,
//        noDoubleQuote,
//        foundDoubleQuote,
//        foundEndDoubleQuote,
//        noBackslash,
//        foundBackslash,
//        escapeComplete,

        //等待读取状态
        // 1. 发送文件事件触发进入读取文件状态
        config.configure(State.WAITING)
                .permit(Event.sendFile, State.READING_FILE,new ReadfileActon())
                .ignore(Event.noDoubleQuote)
                .ignore(Event.foundDoubleQuote)
                .ignore(Event.foundEndDoubleQuote)
                .ignore(Event.noBackslash)
                .ignore(Event.foundBackslash)
                .ignore(Event.escapeComplete);

        //读取文件状态
        //1. 未发现双引号事件触发保持当前状态
        //2. 发现双引号事件触发进入读取字符串状态
        config.configure(State.READING_FILE)
                .ignore(Event.noDoubleQuote)
                .permit(Event.foundDoubleQuote, State.READING_STRING,new ReadStringStartActon())
                .ignore(Event.sendFile)
                .ignore(Event.foundEndDoubleQuote)
                .ignore(Event.noBackslash)
                .ignore(Event.foundBackslash)
                .ignore(Event.escapeComplete);

        //读取字符串状态
        //1. 未发现反斜杠事件触发保持当前状态
        //2. 发现反斜杠事件触发进入转义状态
        //3. 发现结束双引号事件触发返回读取文件状态
        config.configure(State.READING_STRING)
                .ignore(Event.noBackslash)
                .permit(Event.foundBackslash, State.ESCAPING,new EscapeStartAction())
                .permit(Event.foundEndDoubleQuote, State.READING_FILE,new ReadStringEndActon())
                .ignore(Event.sendFile)
                .ignore(Event.noDoubleQuote)
                .ignore(Event.foundDoubleQuote)
                .ignore(Event.escapeComplete);

        //转义状态
        //1. 转义完成事件触发返回读取字符串状态
        config.configure(State.ESCAPING)
                .permit(Event.escapeComplete, State.READING_STRING,new EscapeEndAction())
                .ignore(Event.sendFile)
                .ignore(Event.noDoubleQuote)
                .ignore(Event.foundDoubleQuote)
                .ignore(Event.foundEndDoubleQuote)
                .ignore(Event.noBackslash)
                .ignore(Event.foundBackslash);

        // 创建状态机实例，初始状态为输入密码状态
        stateMachine = new StateMachine<>(State.WAITING, config);
    }

    public void fire(Event event) {
        this.stateMachine.fire(event);
    }

    public State getCurrentState() {
        return this.stateMachine.getState();
    }

    public void printCurrentState() {
        System.out.println("Current State: " + stateMachine.getState());
    }

}
